package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.FunctionTime;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * 设置多功能卡时间
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetFunctionTime implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送卡序号
         */
        private Integer prcIndex;
        /**
         * 网卡号
         */
        private Integer portIndex;
        /**
         * 多功能卡序号
         */
        private Integer functionIndex;
        /**
         * 设置的时间
         */
        private FunctionTime functionTime;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 返回设置后的时间
         */
        private FunctionTime functionTime;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetFunctionTime.InputMsg.Builder inputMsg =
                CompleteInstruction.SetFunctionTime.InputMsg.newBuilder();
        SetFunctionTime.InputMsg thisInput = this.getInput();
        inputMsg.setPrcIndex(thisInput.getPrcIndex());
        inputMsg.setPortIndex(thisInput.getPortIndex());
        inputMsg.setFunctionIndex(thisInput.getFunctionIndex());
        inputMsg.setFunctionTime(thisInput.getFunctionTime().toProto());
        final CompleteInstruction.SetFunctionTime.Builder builder =
                CompleteInstruction.SetFunctionTime.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetFunctionTime byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetFunctionTime setFunctionTime = null;
        try {
            setFunctionTime = CompleteInstruction.SetFunctionTime.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setFunctionTime)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetFunctionTime.OutputMsg outputMsg = new SetFunctionTime.OutputMsg();
        CompleteInstruction.SetFunctionTime.OutputMsg output = setFunctionTime.getOutput();
        FunctionTime functionTime = new FunctionTime().formProto(output.getFunctionTime());
        outputMsg.setFunctionTime(functionTime);
        this.setOutput(outputMsg);
        return this;
    }
}

